﻿USE [TanNamChinh]
/*
Tổng hợp các script dùng thường xuyên trong CSDL
*/
--=============================================
--1. Copy Diagram
--=============================================
-- List all database diagrams
SELECT * FROM [SourceDB].[dbo].sysdiagrams

-- Insert a particular database diagram
DECLARE @SourceDiagramId int = 1
INSERT INTO [DestinationDB].[dbo].sysdiagrams
SELECT [name],diagram_id , version,definition from [SourceDB].[dbo].sysdiagrams
WHERE diagram_id = @SourceDiagramId

-- Update the principal id (no idea why, but it set the owner as some asp_net user
UPDATE [DestinationDB].[dbo].sysdiagrams
SET principal_id = 1
--=============================================
-- Ví dụ:
SELECT * FROM [TanNamChinh_New].[dbo].sysdiagrams
INSERT INTO [TanNamChinh_New].[dbo].sysdiagrams
SELECT [name],diagram_id , version,definition from [TanNamChinh].[dbo].sysdiagrams
WHERE diagram_id = 2

-- Update the principal id (no idea why, but it set the owner as some asp_net user
UPDATE [TanNamChinh_New].[dbo].sysdiagrams
SET principal_id = 1
--=============================================

--=============================================
--Cập nhật relation ship giữa 2 table
--=============================================
--Thêm cascade delete tblHBL sẽ delete tblHBLExchange
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_tblHBLExchange_tblHBL]') AND parent_object_id = OBJECT_ID(N'[dbo].[tblHBLExchange]'))
ALTER TABLE [dbo].[tblHBLExchange] DROP CONSTRAINT [FK_tblHBLExchange_tblHBL]
GO
ALTER TABLE [dbo].[tblHBLExchange]  WITH CHECK ADD  CONSTRAINT [FK_tblHBLExchange_tblHBL] FOREIGN KEY([ID_HBL])
REFERENCES [dbo].[tblHBL] ([ID_HBL])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[tblHBLExchange] CHECK CONSTRAINT [FK_tblHBLExchange_tblHBL]
--=============================================
